package com.ruoyi.utils.txy;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.SSL.domain.SslApply;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUtils;
import com.tencentcloudapi.cdn.v20180606.CdnClient;
import com.tencentcloudapi.cdn.v20180606.models.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

public class TxyCdnDomainUtils {

    /**
     * 创建请求对象
     */
    private static CdnClient createCli(String secretId, String secretKey) {
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("cdn.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        return new CdnClient(cred, "", clientProfile);
    }

    public static AjaxResult listCdnDomain(String secretId, String secretKey, String serviceType) {
        try {
            //获得请求客户端对象
            CdnClient client = createCli(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeDomainsRequest req = new DescribeDomainsRequest();
            DomainFilter domainFilter = new DomainFilter();
            domainFilter.setName("serviceType");
            domainFilter.setValue(new String[]{serviceType});
            req.setFilters(new DomainFilter[]{domainFilter});
            // 返回的resp是一个DescribeDomainsResponse的实例，与请求对象对应
            DescribeDomainsResponse resp = client.DescribeDomains(req);
            return AjaxResult.success("查询腾讯云Cdn域名成功！",resp.getDomains());
        } catch (Exception e) {
            return AjaxResult.error("查询腾讯云Cdn域名错误："+e.getMessage());
        }
    }


    public static AjaxResult putCdnDomainCert(String secretId, String secretKey, SslApply sslApply, String domainTxt, String ptName){
        try {
            //获得请求客户端对象
            CdnClient client = createCli(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            UpdateDomainConfigRequest req = new UpdateDomainConfigRequest();
            req.setDomain(domainTxt);
            Https https = new Https();
            https.setSwitch("on");
            ServerCert serverCert = new ServerCert();
            //重新计算信息  读取文件数据
            String[] fileArray = sslApply.getSslFilePath().split(",");
            serverCert.setPrivateKey(FileUtils.readFileContent(fileArray[0]));
            serverCert.setCertificate(FileUtils.readFileContent(fileArray[1]));
            String certName = sslApply.getCertificateDomain()+"_"+ptName+"_"+ RandomUtil.randomStringUpper(9);
            serverCert.setCertName(certName);
            serverCert.setMessage(certName);
            https.setCertInfo(serverCert);
            //赋值证书信息
            req.setHttps(https);
            // 返回的resp是一个UpdateDomainConfigResponse的实例，与请求对象对应
            UpdateDomainConfigResponse resp = client.UpdateDomainConfig(req);
            Console.log(JSONUtil.toJsonStr(resp));
            return AjaxResult.success("更新腾讯云Cdn域名证书成功！");
        } catch (Exception e) {
            return AjaxResult.error("更新腾讯云Cdn域名证书错误："+e.getMessage());
        }
    }

}
